<template>
  <div class="base-container" id="main">
    <t-space direction="vertical">
      <t-form labelAlign="top" ref="form" :data="formData" :rules="rules" class="step-form table-form"
              labelWidth="140px">
        <t-row>
          <t-col :span="2" class="title"><span class="require">*</span>年度</t-col>
          <t-col :span="2">
            <t-form-item label="年度" name="year">
              <t-select clearable v-model="formData.year" :options="yearOption" />
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <t-row>
        <t-col :span="1"></t-col>
        <t-col :span="10">
          <t-table
            rowKey="index"
            :data="data"
            :columns="columns"
            stripe
            table-layout="auto"
            size="small"
            hover
            tableLayout="fixed"
            bordered
            cellEmptyContent="-"
          >
            <template #name="{row}">
              <p :class="row.class">{{ row.name }}</p>
            </template>
            <template #num="{row}">
              <!-- :disabled="preData.inputDisabled.includes(row.num)" -->
              <t-input-number v-if="row.num !== ''" min="0" size="large" v-model="formData[`${row.num}`]"></t-input-number>
            </template>
            <template #footerSummary>
              <div class="t-table__row-filter-inner">
                说明：1. 主要平衡关系：
                <br/>        KJ100＝KJ110＋KJ120＋KJ130；
                <br/>        KJ200＝KJ210＋KJ220＋KJ230＋KJ233＋KJ240；
                <br/>        KJ110≥KJ111；KJ220≥KJ221；KJ230≥KJ231
                <br/>      2. 经费部分，所有单位均为万元。
              </div>
            </template>
          </t-table>
        </t-col>
        <t-col :span="1"></t-col>
      </t-row>
      <t-row style="margin-top: 20px;text-align: center">
        <t-col :span="12">
          <t-space :size="10">
            <t-button v-if="!isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(1)">{{
                applyLoading ? '提交中...' : '暂存'
              }}
            </t-button>
            <t-button v-if="!isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(2)">{{
                applyLoading ? '提交中...' : '提交'
              }}
            </t-button>
            <t-button v-if="isUpdate" theme="default" :loading="applyLoading" @click="onSubmit(3)">{{
                '取消'
              }}
            </t-button>
            <t-button v-if="isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(4)">{{
                applyLoading ? '修改中...' : '确认'
              }}
            </t-button>
          </t-space>
        </t-col>
      </t-row>
    </t-space>
  </div>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
export default {
  name: "expenditure-add-edit",
  dicts: [],
  components: {
  },
  data() {
    return {
      yearOption: [],
      file_domain: VITE_FILE_DOMAIN,
      deptMenuOptions: [],
      columns: [
        // { colKey: 'index', title: '序号', width: '100' },
        { colKey: 'name', title: '指标名称', },
        { colKey: 'code', title: '代码', align: "center" },
        { colKey: 'jldw', title: '计量单位', align: "center"},
        { colKey: 'num', title: '数量', align: "center"},
      ],
      rules: {
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
          }
        ],
      },
      data: [],
      preData: {
        name: [
          '一、当年科普经费筹集额',
          '  1.政府拨款',
          '    其中：科普专项经费',
          '  2.捐赠',
          '  3.自筹资金',
          '二、当年科普经费使用额',
          '  1.行政支出',
          '  2.科普活动支出',
          '    其中：科技活动周经费支出',
          '  3.科普场馆基建支出',
          '    其中：政府拨款支出',
          '  4.科普展品、设施支出',
          '  5.其他支出',
        ],
        code:[
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
          '万元',
        ],
        jldw: [
          'KJ100',
          'KJ110',
          'KJ111',
          'KJ120',
          'KJ130',
          'KJ200',
          'KJ210',
          'KJ220',
          'KJ221',
          'KJ230',
          'KJ231',
          'KJ233',
          'KJ240',
        ],
        num: [
          'kpjfcj',
          'zfbkcj',
          'kpzxjfcj',
          'jzcj',
          'zccj',
          'kpjfsy',
          'xzzcsy',
          'kphdzcsy',
          'kjhdzcsy',
          'kpcgzcsy',
          'zfbkzcsy',
          'kpzpzcsy',
          'qtzcsy',
        ],
        class: [
          '',
          'indent2',
          'indent4',
          'indent2',
          'indent2',
          '',
          'indent2',
          'indent2',
          'indent4',
          'indent2',
          'indent4',
          'indent2',
          'indent2',
        ],
        inputDisabled: []
      },
      applyLoading: false,
      formData: {
        localId: '',
        year: new Date().getFullYear(),
        kpjfcj: 0,
        zfbkcj: 0,
        kpzxjfcj: 0,
        jzcj: 0,
        zccj: 0,
        kpjfsy: 0,
        xzzcsy: 0,
        kphdzcsy: 0,
        kjhdzcsy: 0,
        kpcgzcsy: 0,
        zfbkzcsy: 0,
        kpzpzcsy: 0,
        qtzcsy: 0,
      },
      isUpdate: false,
    }
  },
  created() {
    for (let i = 2030; i >= 2000; i--) {
      this.yearOption.push({key: i, label: i.toString(), value: i.toString()})
    }
    const taskId = this.$route.params && this.$route.params.taskId;
    this.isUpdate = this.$route.query.isUpdate === 'true'
    Object.assign(this.formData, this.$route.query.currentRow)
    if (!taskId) {
      this.$message.error("缺少id");
      this.close();
      return false;
    }
    this.formData.localId = taskId

    for (let i = 0; i < this.preData.name.length; i++) {
      this.data.push({
        index: i + 1,
        name: this.preData.name[i],
        code: this.preData.code[i],
        jldw: this.preData.jldw[i],
        num: this.preData.num[i],
        class: this.preData.class[i]
      })
    }
  },
  methods: {
    onSubmit(type) {
      if (!this.formData.year) {
        this.$message.error('请选择年份！');
        return
      }
      if (type === 1) {
        this.$api.kpzh.expenditure.add(this.formData).then(res => {
          this.$message.success('暂存成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/expenditure/expenditure-my"});
        })
      } else if (type === 2) {
        this.$api.kpzh.expenditure.submit(this.formData).then(res => {
          this.$message.success('提交成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/expenditure/expenditure-my"});
        })
      } else if (type === 3) {
        this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/expenditure/expenditure-my"});
      } else if (type === 4) {
        this.$api.kpzh.expenditure.update(this.formData).then(res => {
          this.$message.success('修改成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/expenditure/expenditure-my"});
        })
      } else {
        this.$message.error('type错误！');
      }
    },
    close() {
      this.$tab.closeOpenPage({path: "/kpzh/expenditure/expenditure-my"});
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .t-table__empty {
  min-height: initial;
}

</style>
